<?php
class Shopping_IndexController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 8,
									'pageRange' => 4,
								  ); 
	protected $_namespace;
	
	public function init(){
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	

							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		$template_path = TEMPLATE_PATH . "/public/books";
		$this->loadTemplate($template_path,'template.ini','template');
		
	}
	
	public function indexAction() {
		$tblProduct = new Shopping_Model_Item();
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-index'));
		
		$totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-index'));
		
		$paginator = new Zendvn_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
		
		
	}	

	public function categoryAction(){
		$tblProduct = new Shopping_Model_Item();
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-category'));
		
		$totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-category'));
		//echo $totalItem;
		$paginator = new Zendvn_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
	/*	echo '<pre>';
		print_r($this->_arrParam);
		echo '</pre>';*/
	}
	
	public function detailAction(){
		
		$tblProduct = new Shopping_Model_Item();
		$this->view->Item = $tblProduct->getItem($this->_arrParam,array('task'=>'public-detail'));
		$this->view->Title = $this->view->Item['name'] . ' | Book shop | www.zend.vn';
		$this->view->headTitle($this->view->Title,true);
		
	}
	
	public function viewCartAction(){
		$yourCart = new Zend_Session_Namespace('cart');
		
		if($this->_request->isPost()){
			$itemProduct = $this->_arrParam['itemProduct'];
			
			if(count($itemProduct)>0)
				foreach($itemProduct as $key => $val){
					if($val == 0 ){
						unset($itemProduct[$key]);
				}
			}
			
			$yourCart->cart = $itemProduct;
		}
		
		
		
		$ssInfo = $yourCart->getIterator();
		
		$tblProduct = new Shopping_Model_Item();
		$this->_arrParam['cart'] = $ssInfo['cart'];
		
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'view-cart'));
		$this->view->cart =  $ssInfo['cart'];
		
		
		
	}
	
	public function addItemAction(){
				
		$yourCart = new Zend_Session_Namespace('cart');
		
		$ssInfo = $yourCart->getIterator();
		
		$filter = new Zend_Filter_Digits();
		$id = $filter->filter($this->_arrParam['id']);
			
		if(count($yourCart->cart) == 0){
			
			$cart[$id] = 1;
			$yourCart->cart = $cart;
		}else{
			//echo '<br>Trong gio hang da co san pham';
			$tmp = $ssInfo['cart'];
			if(array_key_exists($id,$tmp) == true){
				$tmp[$id] = $tmp[$id] + 1;
			}else{
				$tmp[$id] = 1;
			}
			
			$yourCart->cart = $tmp;
			
		}
		
		
		$this->_redirect($this->_currentController . '/view-cart');
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function orderAction(){
		$yourCart = new Zend_Session_Namespace('cart');
			
		$ssInfo = $yourCart->getIterator();
		
		$tblProduct = new Shopping_Model_Item();
		$this->_arrParam['cart'] = $ssInfo['cart'];
		
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'view-cart'));
		$this->view->cart =  $ssInfo['cart'];
		
		if($this->_request->isPost()){
			$tblInvoice = new Shopping_Model_Invoice();
			$invoice_id = $tblInvoice->saveItem($this->_arrParam,array('task'=>'public-order'));
			
			$tblInvoiceDetail = new Shopping_Model_InvoiceDetail();
			$this->_arrParam['invoice_id'] = $invoice_id;
			$tblInvoiceDetail->saveItem($this->_arrParam);
			$yourCart->unsetAll();
		}
	}
}










